/**
 * Created by Administrator on 15-12-2.
 */
var ListDoAction = RichBase.extend({
	EVENTS : {
		"click" : {
			".doBtn" : "onDoBtnClick"
		}
	},
	init : function(){
		this.container = $("#resultListUl");
	},
	onDoBtnClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		var ordernum = tarBtn.attr("data-ordernum");
		var action = tarBtn.attr("data-action");
		if(!ordernum || !action || tarBtn.hasClass("loading")) return false;
		if(action=="orderAlter"){ //修改

		}else if(action=="orderCancel"){ //取消

		}else if(action=="orderCheck"){ //验证

		}else if(action=="orderResend"){ //重发短信
			that.resendMsg(tarBtn,ordernum);
		}
	},
	//取消订单
	cancelOrder : function(tarBtn,ordernum){
		var result = confirm("确定要取消该订单？");
		if(!result) return false;
		PFT.Ajax({
			url : "call/handle.php?from=order_cancel",
			type : "get",
			dataType : "json",
			data : {
				ordernum : ordernum
			},
			loading : function(){
				tarBtn.html("正在取消...").addClass("loading");
			},
			removeLoading : function(){
				tarBtn.html("取消").removeClass("loading");
			},
			timeout : function(res){ alert("请求起时，请稍后重试")},
			serverError : function(res){ alert("请求出错，请稍后重试")}
		},function(res){
			if(res.outcome==1){
				PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">取消成功</p>');
				tarBtn.parents(".col").siblings('.col_5').find(".tdCon").html("<em style='color:#bdbdbd'>已取消</em>");
				tarBtn.siblings(".orderAlipay").remove();
				tarBtn.siblings(".orderAlter").remove();
				tarBtn.siblings(".orderCheck").remove();
				tarBtn.siblings(".orderResend").remove();
				tarBtn.remove();
				tarBtn.parents(".listItem").addClass("disable")
			}else{
				var txt = res.msg || "";
				PFT_GLOBAL.U.Alert("fail",'<p style="width:400px;">取消失败 '+txt+'</p>');
			}
		})
	},
	//重发短信
	resendMsg : function(tarBtn,ordernum){
		if(!confirm("确定要重发短信？")) return false;
		PFT.Ajax({
			url : "call/handle.php?from=order_resend",
			type : "get",
			dataType : "json",
			data : {
				ordernum : ordernum
			},
			loading : function(){
				tarBtn.text("正在重发...").addClass("loading");
			},
			removeLoading : function(){
				tarBtn.text("重发短信").removeClass("loading");
			},
			timeout : function(res){ alert("请求起时，请稍后重试")},
			serverError : function(res){ alert("请求出错，请稍后重试")}
		},function(res){
			if(res.outcome==1){
				PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">短信已重发</p>');
			}else{
				alert(res.msg || "");
			}
		})
	},
	dos : function(){
		//表格操作： 取消订单 重发短信 修改订单
		var TableList = RichBase.extend({
			statics : {
				dialog : null,
				isPositiveNum : function(count){
					count = String(count);
					var type="^[0-9]*[1-9][0-9]*$";
					var re = new RegExp(type);
					if(count.match(re) == null){
						return false;
					}
					return true;
				}
			},
			EVENTS : {
				"click" : {
					".orderHandle" : "cancelOrder",
					".resend" : "resendMsg",
					".alter" : "openDialog"
				}
			},
			init : function(opt){
				this.tableContainer = opt.container;
			},
			//取消订单
			cancelOrder : function(that,e){
				e.preventDefault();
				var tarBtn = $(e.currentTarget);
				var result = confirm("确定要取消该订单？");
				if(!result) return false;
				var success = function(){
					var ordernum = tarBtn.attr("data-ordernum");
					PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">取消成功</p>');
					tarBtn.parent().siblings('.col_6').html("<em class='red'>已取消</em>");
					var trs = $("#listTable").children("tbody").children("[data-ordernum="+ordernum+"]");
					trs.addClass("disable");
					tarBtn.remove();
				};
				$.ajax({
					url : tarBtn.attr("href"),
					dataType : "json",
					beforeSend : function(){
						tarBtn.html("正在取消...");
					},
					success : function(data){
						tarBtn.html("取消");
						if(data.outcome==1){
							success();
						}else{
							var txt = data.msg || "";
							PFT_GLOBAL.U.Alert("fail",'<p style="width:400px;">取消失败 '+txt+'</p>');
						}
					},
					error : function(){
						tarBtn.html("取消");
					}
				})
			},
			//重发短信
			resendMsg : function(that,e){
				e.preventDefault();
				var that = this;
				var tarBtn = $(e.currentTarget);
				if(!confirm("确定要重发短信？")) return false;
				$.ajax({
					url : tarBtn.attr("href"),
					dataType : "json",
					beforeSend : function(){
						tarBtn.text("正在重发...")
					},
					success : function(data){
						tarBtn.text("重发");
						if(data.outcome==1){
							PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">重发成功</p>');
						}else{
							var txt = data.msg || "";
							alert(txt);
						}
					},
					error : function(){
						tarBtn.text("重发")
					}
				})
				return false;
			},
			//修改票数
			modifyTicket : function(){
				var that = this;
				var tarBtn = this.statics.modifyBtn;
				var modifyData = $("#ticModForm").serialize();
				$.ajax({
					url : "call/handle.php?from=order_alter",
					type : "post",
					dataType : "json",
					data : modifyData,
					beforeSend : function(){
						tarBtn.text("正在修改..");
					},
					success:function(data){
						tarBtn.text("修改");
						if(data.outcome==1){
							PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">修改成功</p>');
							var ordernum = $("#modifyTicket_ordernum_hidInp").val();
							$("#ticModForm").find(".numInp").each(function(){
								var tarInp = $(this);
								var tid = tarInp.attr("data-tid");
								var tnum = tarInp.val();
								$("#"+ordernum+"_"+tid).text(tnum);
							})
							var tr = $("#listTable").children("tbody").children("[data-ordernum="+ordernum+"]").first();
							var total = tr.find(".col_7 .totalPrice i");
							data.price && total.text(data.price);
							var result = [];
							$("#ticModListUl").children().each(function(){
								var li = $(this);
								var tname = li.children(".tname").text();
								var inp = li.find(".numInp");
								var tid = inp.attr("data-tid");
								var num = inp.val();
								result.push(tname+"|"+num+"|"+tid);
							})
							that.statics.modifyBtn.attr("data-ticket",result.join("&"));
						}else{
							var txt = data.msg || "";
							alert(txt);
						}
					},
					error : function(){
						PFT_GLOBAL.U.Alert("fail",'<p style="width:200px;">修改失败，请稍后重试</p>');
					}
				})
			},
			initDialog : function(con){
				var self = this;
				this.statics.dialog = new PFT.Dialog({
					content : con,
					EVENTS : {
						"click" : {
							//确认修改
							".modTicketDialogCon .modifyBtn" : function(that,e){
								e.preventDefault();
								that.close();
								self.modifyTicket();
							},
							//取消修改
							".modTicketDialogCon .cannelModifyBtn" : function(that,e){
								e.preventDefault();
								that.close();
							},
							//减票数
							".modTicketDialogCon .minuBtn" : function(that,e){
								var tarBtn = $(e.currentTarget);
								if(tarBtn.hasClass("disable")) return false;
								var tarInp = tarBtn.parent().children(".numInp");
								var val = tarInp.val();
								var min = tarInp.attr("data-min");
								var addBtn = tarBtn.parent().children(".addBtn");
								val--;
								addBtn.removeClass("disable");
								if(val>=min){
									tarInp.val(val);
									val==min ? tarBtn.addClass("disable") : tarBtn.removeClass("disable");
								}else{
									tarBtn.addClass("disable");
								}
							},
							//加票数
							".modTicketDialogCon .addBtn" : function(that,e){
								var tarBtn = $(e.currentTarget);
								if(tarBtn.hasClass("disable")) return false;
								var tarInp = tarBtn.parent().children(".numInp");
								var val = tarInp.val();
								var max = tarInp.attr("data-max");
								var minuBtn =  tarBtn.parent().children(".minuBtn");
								val++;
								minuBtn.removeClass("disable");
								if(val<=max){
									tarInp.val(val);
									val==max ? tarBtn.addClass("disable") : tarBtn.removeClass("disable");
								}else{
									tarBtn.addClass("disable");
								}
							}
						},
						"focus" : {
							".modTicketDialogCon .numInp" : function(that,e){
								var tarInp = $(e.currentTarget);
								tarInp.attr("data-lastval",tarInp.val());
							}
						},
						"blur" : {
							".modTicketDialogCon .numInp" : function(that,e){
								var tarInp = $(e.currentTarget);
								var val = tarInp.val() * 1;
								var lastVal = tarInp.attr("data-lastval") * 1;
								var maxVal = tarInp.attr("data-max") * 1;
								var minVal = tarInp.attr("data-min") * 1;
								if(!self.statics.isPositiveNum(val)){
									tarInp.val(lastVal);
								}else if(val<minVal){
									tarInp.val(minVal);
								}else if(val>maxVal){
									tarInp.val(maxVal);
								}
							}
						}
					}
				});
			},
			//点击弹出票修改dialog
			openDialog : function(that,e){
				e.preventDefault();
				var tarBtn = $(e.currentTarget);
				var title = tarBtn.attr("data-title");
				var api = tarBtn.attr("href");
				var ordernum = tarBtn.attr("data-ordernum");
				var tickets = tarBtn.attr("data-ticket").split("&");
				var dialogTpl = $("#modTicDiaTpl").html();
				var con = that.parseTemplate(dialogTpl,{ticketList:""});
				var listTpl = $("#modTicDiaList").html();
				if(!that.statics.dialog) that.initDialog(con);
				that.statics.dialog.open({
					title : title,
					content : con,
					offset : -100,
					onBeforeOpen : function(){
						var list = "";
						for(var i in tickets){
							var t = tickets[i].split("|");
							var tname = t[0];
							var tnum = t[1];
							var tid = t[2];
							var readonly = tnum==1 ? "readonly" : "";
							list += that.parseTemplate(listTpl,{
								tname : tname,
								tnum : tnum,
								tid : tid,
								readonly : readonly
							});
						}
						that.statics.modifyBtn = tarBtn;
						$("#modifyTicket_ordernum_hidInp").val(ordernum);
						$("#ticModListUl").html(list);
						$("#ticModForm").attr("action",api);
					}
				})
			}
		});

		$(function(){
			new TableList({ container:$("#listTable")})
		})
	}
});
module.exports = ListDoAction;